package awesome.cpu.structure;

public class MuxA extends NonSequentialComponent {
	
	public final static int KEY_ALU_SRCA = 1;
	private int mALUSrcA;
	
	public final static int KEY_PC = 2;
	private int mPC;
	public final static int KEY_REGA = 3;
	private int mRegA;
	
	public final static int KEY_SELECTED = 5;
	//private int mSelected;
	
	private final static MuxA mInstance = new MuxA();
	
	private MuxA() {}
	
	public static MuxA getInstance() {
		return mInstance;
	}

	@Override
	boolean setData(int keyInterface, int data) {
		switch (keyInterface) {
		case KEY_ALU_SRCA:
			mALUSrcA = data;
			return true;
		case KEY_PC:
			mPC = data;
			return true;
		case KEY_REGA:
			mRegA = data;
			return true;
		}
		return false;
	}

	@Override
	int getData(int keyInterface) {
		
		BusMonitor.transferData(Control.getInstance(), Control.KEY_ALU_SRC_A,
				this, KEY_ALU_SRCA);
		BusMonitor.transferData(PC.getInstance(), PC.KEY_PC_OUT,
				this, KEY_PC);
		BusMonitor.transferData(RegisterA.getInstance(), RegisterA.KEY_REGA_OUT,
				this, KEY_REGA);
		
		switch (keyInterface) {
		case KEY_SELECTED:
			return select();
		}
		return 0;
	}

	private int select() {
		switch (mALUSrcA) {
		case 0:
			return mPC;
		case 1:
			return mRegA;
		}
		return 0;
	}
}
